﻿@model Apps.Models.EXTENDFIELD_REFERENCEVALUE
@using Apps.Web.Core;
@using Apps.Locale;
@using Apps.Models.Sys;
@using Apps.Models.Enum;
@using Apps.Common;
@{
    ViewBag.Title = "修改";
    Layout = "~/Views/Shared/_Index_LayoutEdit.cshtml";
    List<permModel> perm = null;
    //var fModel = ViewBag.FieldModel as Apps.Models.HC_EXTEND_FIELD_VALUE;/*TEST_TYPE>10*/
}

<script type="text/javascript">
    function GeChangedData() {
        var model = {
            ID: $("#ID").val(),
            TITLE: $("#TITLE").val(),
            NAME: $("#NAME").val(),
            CONTROL_TYPE: $("#CONTROL_TYPE").val(),
            IS_REQUIRED: $("#IS_REQUIRED").val(),
            ENABLE: $("#ENABLE").val(),
            SORT: $("#SORT").val(),
            REMARK: $("#REMARK").val(),
            ReferenceValues: JSON.stringify(changeRow)
        };
        return model;
    }
    $(function () {
        $("#btnSave").click(function () {
            if (editIndex != undefined) {
                return window.parent.frameReturnByMes("存在未保存的参考指标数据，请先保存");
            }
            if ($("form").valid()) {
                $.ajax({
                    url: "@Url.Action("Edit")",
                    type: "Post",
                    data: GeChangedData(),
                    dataType: "json",
                    success: function (data) {
                        if (data.type == 1) {
                            window.parent.frameReturnByMes(data.message);
                            window.parent.frameReturnByReload(false);
                            window.parent.frameReturnByClose()
                        }
                        else {
                            window.parent.frameReturnByMes(data.message);
                        }
                    }
                });
            }
            return false;
        });
        $("#btnReturn").click(function () {
            window.parent.frameReturnByClose();
        });
    });
</script>
<div class="mvctool bgb">
    @Html.ToolButton("btnSave", "fa fa-save", Resource.Save, ref perm, "Save", true)
    @Html.ToolButton("btnReturn", "fa fa-reply", Resource.Reply, false)
</div>
@using (Html.BeginForm())
{
    <input type="hidden" id="ID" name="ID" value="@Model.ID" />
    // @Html.HiddenFor(model => model.KKSCODE)
    <table class="formtable">
        <tbody>
            <tr>
                <th style="width:80px">
                    <label for="CONTROL_TYPE"><font color="red">*</font>控件类型</label>：
                </th>
                <td>
                    <select name="CONTROL_TYPE" id="CONTROL_TYPE">
                        <option value="@((int)HcControlTypeEnum.DateTime)" @Utils.CheckedSelected(@Model.CONTROL_TYPE.ToString(), "3")>时间</option>
                        <option value="@((int)HcControlTypeEnum.Number)" @Utils.CheckedSelected(@Model.CONTROL_TYPE.ToString(), "2")>数值</option>
                        <option value="@((int)HcControlTypeEnum.SingleText)" @Utils.CheckedSelected(@Model.CONTROL_TYPE.ToString(), "1")>单行文本</option>
                    </select>
                </td>
                <th style="width:80px">
                    <label for="ENABLE"><font color="red">*</font>启用状态</label>：
                </th>
                <td>
                    <select id="ENABLE" name="ENABLE" autocomplete="off"><option value="1" @Utils.CheckedSelected(@Model.ENABLE.ToString(), "1")>启用</option><option value="0" @Utils.CheckedSelected(@Model.ENABLE.ToString(), "0")>禁用</option></select>
                </td>
            </tr>
            <tr>
                <th style="width:80px">
                    <label for="TITLE"><font color="red">*</font>标题显示</label>：
                </th>
                <td>
                    <input class="text-box single-line" data-val="true" data-val-required="标题显示字段是必需的。" id="TITLE" name="TITLE" type="text" value="@Model.TITLE" autocomplete="off">
                </td>
                <th style="width:80px">
                    <label for="NAME"><font color="red">*</font>列名</label>：
                </th>
                <td>
                    <input class="text-box single-line" data-val="true" data-val-required="列名字段是必需的。" readonly="readonly" id="NAME" name="NAME" type="text" value="@Model.NAME" autocomplete="off">
                </td>
            </tr>
            <tr>
                <th style="width:80px">
                    <label for="SORT"><font color="red">*</font>排序</label>：
                </th>
                <td>
                    <input class="text-box single-line" id="SORT" name="SORT" type="text" value="@Model.SORT" autocomplete="off">
                </td>
                <th style="width:80px">
                    <label for="IS_REQUIRED"><font color="red">*</font>是否必填</label>：
                </th>
                <td>
                    <select id="IS_REQUIRED" name="IS_REQUIRED" autocomplete="off"><option @Utils.CheckedSelected(@Model.ENABLE.ToString(), "1") value="1">是</option><option @Utils.CheckedSelected(@Model.ENABLE.ToString(), "0") value="0">否</option></select>
                </td>
            </tr>
            <tr>
                <th style="width:80px">
                    <label for="REMARK">备注</label>：
                </th>
                <td colspan="4">
                    <textarea cols="20" id="REMARK" name="REMARK" style="width:710px;height:30px;" autocomplete="off">@Model.REMARK</textarea>
                </td>
            </tr>
            <tr>
                <td colspan="5" style="vertical-align: top; ">
                    <table id="List"></table>
                </td>
            </tr>
        </tbody>
    </table>
    @Html.ValidationSummary()
}
<script type="text/javascript">
    var editIndex = undefined;
    var changeRow=[];
    var effectRow = new Object();
    function endEditing() {
        if (editIndex == undefined) { return true }
        if ($('#List').datagrid('validateRow', editIndex)) {
            $('#List').datagrid('endEdit', editIndex);
            editIndex = undefined;
            return true;
        } else {
            return false;
        }
    }
    function onClickRow(index) {
        if (editIndex != index) {
            if (endEditing()) {
                $('#List').datagrid('selectRow', index).datagrid('beginEdit', index);
                editIndex = index;
            } else {
                $('#List').datagrid('selectRow', editIndex);
            }
        }
    }

    function onClickCell(index, field) {
        if (editIndex != index) {
            if (endEditing()) {
                $('#List').datagrid('selectRow', index)
                    .datagrid('beginEdit', index);
                var ed = $('#List').datagrid('getEditor', { index: index, field: field });
                if (ed) {
                    ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
                }
                editIndex = index;
            } else {
                setTimeout(function () {
                    $('#List').datagrid('selectRow', editIndex);
                }, 0);
            }
        }
    }
    function onEndEdit(index, row) {
        var ed = $(this).datagrid('getEditor', {
            index: index,
            field: 'ID'
        });
        //row.productname = $(ed.target).combobox('getText');
    }
    function append() {
        if (endEditing()) {
            $('#List').datagrid('appendRow', { ID:'@ResultHelper.NewId', BASEVALUE: '', MINVALUE: 0.000, MAXVALUE: 0.000 });
            editIndex = $('#List').datagrid('getRows').length - 1;
            $('#List').datagrid('selectRow', editIndex)
                .datagrid('beginEdit', editIndex);
        }
    }
    function acceptit() {
        if (endEditing()) {
            var changed = $('#List').datagrid('getChanges');
            debugger;
            if (changed.length > 0) {
                changeRow = new Array();
                for (var i = 0; i < changed.length; i++) {
                    if (dataCheck(changed[i].MAXVALUE) && dataCheck(changed[i].MINVALUE)) {
                        var changedValue = { "ID": changed[i].ID, "BASEVALUE": changed[i].BASEVALUE, "MAXVALUE": changed[i].MAXVALUE, "MINVALUE": changed[i].MINVALUE };
                        changeRow.push(changedValue);
                    }
                }
            }
        }
    }
    function reject() {
        $('#List').datagrid('rejectChanges');
        editIndex = undefined;
    }
    function getChanges() {
        var rows = $('#List').datagrid('getChanges');
        alert(rows.length + ' rows are changed!');
    }
    function dataCheck(values) {
        if (values != '0.000' && values != '0' && values != '' && values != undefined)
        {
            return true;
        }
        return false;
    }

    $(function () {
        var filedId = $("#ID").val();
        $('#List').datagrid({
            url: '@Url.Action("GetListDetail")?filedId=' + filedId,
            width: SetGridWidthSub(30),
            methord: 'post',
            height: 550,
            fitColumns: true,
            sortName: 'ID',
            sortOrder: 'desc',
            idField: 'ID',
            striped: true, //奇偶行是否区分
            singleSelect: true,
            rownumbers: true,//行号
            onClickRow: onClickRow,
            onEndEdit: onEndEdit,
            onLoadSuccess: function (data) {
                var editData = data.rows.myfilter(function (item, inde, array)
                {
                    return item.ID != '' && item.ID != null
                }
                );
                for (var i = 0; i < editData.length; i++) {
                    if (dataCheck(editData[i].MAXVALUE) && dataCheck(editData[i].MINVALUE))
                    {
                        var changedValue = { "ID": editData[i].ID, "BASEVALUE": editData[i].BASEVALUE, "MAXVALUE": editData[i].MAXVALUE, "MINVALUE": editData[i].MINVALUE };
                        changeRow.push(changedValue);
                    }
                }
            },
            columns: [[
                { field: 'ID', title: 'ID', width: 20, hidden: true },
                {
                    field: 'BASEVALUE', title: '基值', width: 60,
                    editor: {
                        type: 'textbox'
                    }
                },
                {
                    field: 'MINVALUE', title: '最小值', width: 60,
                    editor: {
                        type: 'numberbox',
                        options: {
                            precision: 3
                        }
                    }
                },
                {
                    field: 'MAXVALUE', title: '最大值', width: 60,
                    editor: {
                        type: 'numberbox',
                        options: {
                            precision: 3
                        }
                    }
                },
            ]],
            toolbar: [
                {
                    text: '添加',
                    iconCls: 'fa fa-plus',
                    handler: function () {
                        append();
                    }
                },
                '-',
                {
                    text: '取消',
                    iconCls: 'fa fa-cancel',
                    handler: function () {
                        reject();
                    }
                }, '-',
                {
                    text: "保存",
                    iconCls: "fa fa-save",
                    handler: function () {
                        acceptit();
                    }
                }],
        });
    });
    Array.prototype.myfilter = function (fun /*, thisp*/) {
        var len = this.length;
        if (typeof fun != "function") {
            throw new TypeError();
        }
        var res = new Array();
        var thisp = arguments[1];
        for (var i = 0; i < len; i++) {
            if (i in this) {
                var val = this[i]; // in case fun mutates this
                if (fun.call(thisp, val, i, this)) {
                    res.push(val);
                }
            }
        }
        return res;
    };
</script>

